package com.ruoyi.coupon.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.check.CheckEmptyUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.coupon.domain.CouponConfig;
import com.ruoyi.coupon.service.ICouponConfigService;
import com.ruoyi.coupon.vo.AddUserCouponVo;
import com.ruoyi.coupon.vo.SelectUserCouponVo;
import com.ruoyi.technician.mapper.TechnicianMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 卡券管理Controller
 *
 * @author ruoyi
 * @date 2025-04-09
 */
@RestController
@RequestMapping("/couponConfig/couponConfig")
public class CouponConfigController extends BaseController {
    @Autowired
    private ICouponConfigService couponConfigService;
    @Resource
    private TechnicianMapper technicianMapper;

    /**
     * 查询卡券管理列表
     */
    @PreAuthorize("@ss.hasPermi('couponConfig:couponConfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(CouponConfig couponConfig) {
        startPage();
        List<CouponConfig> list = couponConfigService.selectCouponConfigList(couponConfig);
        if (!CheckEmptyUtil.isEmpty(list)) {
            for (CouponConfig config : list) {
                if (config.getCouponScopeApplication() != 0) {
                    String tName = technicianMapper.selectTName(config.getCouponScopeApplication());
                    config.setTechnicianName(CheckEmptyUtil.isEmpty(tName) ? "" : tName);
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出卡券管理列表
     */
    @PreAuthorize("@ss.hasPermi('couponConfig:couponConfig:export')")
    @Log(title = "卡券管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CouponConfig couponConfig) {
        List<CouponConfig> list = couponConfigService.selectCouponConfigList(couponConfig);
        if (!CheckEmptyUtil.isEmpty(list)) {
            for (CouponConfig config : list) {
                if (config.getCouponScopeApplication() != 0) {
                    String tName = technicianMapper.selectTName(config.getCouponScopeApplication());
                    config.setTechnicianName(CheckEmptyUtil.isEmpty(tName) ? "" : tName);
                }
            }
        }
        ExcelUtil<CouponConfig> util = new ExcelUtil<CouponConfig>(CouponConfig.class);
        util.exportExcel(response, list, "卡券管理数据");
    }

    /**
     * 获取卡券管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('couponConfig:couponConfig:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        CouponConfig couponConfig = couponConfigService.selectCouponConfigById(id);
        if (!CheckEmptyUtil.isEmpty(couponConfig)) {
            if (couponConfig.getCouponScopeApplication() != 0) {
                String tName = technicianMapper.selectTName(couponConfig.getCouponScopeApplication());
                couponConfig.setTechnicianName(CheckEmptyUtil.isEmpty(tName) ? "" : tName);
            }

        }
        return success(couponConfig);
    }

    /**
     * 新增卡券管理
     */
    @PreAuthorize("@ss.hasPermi('couponConfig:couponConfig:add')")
    @Log(title = "卡券管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CouponConfig couponConfig) {
        return toAjax(couponConfigService.insertCouponConfig(couponConfig));
    }

    /**
     * 修改卡券管理
     */
    @PreAuthorize("@ss.hasPermi('couponConfig:couponConfig:edit')")
    @Log(title = "卡券管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CouponConfig couponConfig) {
        return toAjax(couponConfigService.updateCouponConfig(couponConfig));
    }

    /**
     * 删除卡券管理
     */
    @PreAuthorize("@ss.hasPermi('couponConfig:couponConfig:remove')")
    @Log(title = "卡券管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(couponConfigService.deleteCouponConfigByIds(ids));
    }

    /**
     * 根据优惠券ID 查询添加用户优惠券的数据
     */
    @PreAuthorize("@ss.hasPermi('couponConfig:couponConfig:add')")
    @PostMapping("selectUserCoupon")
    public AjaxResult selectUserCoupon(@RequestBody SelectUserCouponVo requestVo) {
        return success(couponConfigService.selectUserCoupon(requestVo));
    }


    /**
     * 根据优惠券ID 查询添加用户优惠券的数据
     */
    @PreAuthorize("@ss.hasPermi('couponConfig:couponConfig:add')")
    @PostMapping("addUserCoupon")
    public AjaxResult addUserCoupon(@RequestBody AddUserCouponVo requestVo) {
        return success(couponConfigService.addUserCoupon(requestVo));
    }


}
